﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace serial
{

    
    public partial class WaveForm : Form
    {
        public delegate float getdata();
        getdata getpitch = new getdata(szyProtocol.sendpitch);
        getdata getroll = new getdata(szyProtocol.sendroll);
        getdata getyaw = new getdata(szyProtocol.sendyaw);
        getdata getcompass = new getdata(szyProtocol.sendcompass);
        getdata gettemp = new getdata(szyProtocol.sendtemp);
        getdata getdepth = new getdata(szyProtocol.senddepth);
        getdata getelec = new getdata(szyProtocol.sendelec);

        private Queue<float> dataPitch = new Queue<float>(250);
        private Queue<float> dataRoll = new Queue<float>(250);
        private Queue<float> dataYaw = new Queue<float>(250);
        private Queue<float> data4 = new Queue<float>(250);
        private Queue<float> data5 = new Queue<float>(250);
        private Queue<float> data6 = new Queue<float>(250);
        private Queue<float> data7 = new Queue<float>(250);
        private Queue<float> data8 = new Queue<float>(250);


        private static float curPitch;
        private static float curRoll;
        private static float curYaw;
        private static float curData4;
        private static float curData5;
        private static float curData6;
        private static float curData7;
        private static float curData8;

        public static float CurPitch { get => curPitch; set => curPitch = value; }
        public static float CurRoll { get => curRoll; set => curRoll = value; }
        public static float CurYaw { get => curYaw; set => curYaw = value; }
        public static float CurData4 { get => curData4; set => curData4 = value; }
        public static float CurData5 { get => curData5; set => curData5 = value; }
        public static float CurData6 { get => curData6; set => curData6 = value; }
        public static float CurData7 { get => curData7; set => curData7 = value; }
        public static float CurData8 { get => curData8; set => curData8 = value; }

        private int num = 2;
        public int timerInterval = 8;
        public WaveForm()
        {
            InitializeComponent();
            InitChart();
            timerPitch.Interval = timerInterval;
            timerRoll.Interval = timerInterval;
            timerYaw.Interval = timerInterval;
            timerData4.Interval = timerInterval;
            timerData5.Interval = timerInterval;
            timerData6.Interval = timerInterval;
            timerData7.Interval = timerInterval;
            timerData8.Interval = timerInterval;
  
        }

        ChartArea chartArea1 = new ChartArea("C1");
        Series Pitch = new Series("Pitch");
        Series Roll = new Series("Roll");
        Series Yaw = new Series("Yaw");
        Series Data4 = new Series("Data4");
        Series Data5 = new Series("Data5");
        Series Data6 = new Series("Data6");
        Series Data7 = new Series("Data7");
        Series Data8 = new Series("Data8");

        public void RecData(byte[] data)
        {
            
            CheckData(data);
            for (int i = 0; i < data.Length; i++)
            {
                Console.Write(data[i]+"  ");
            }
        }
        public void CheckData(byte[] data)
        {
            if (data.Length == 8)
            {
                if (data[0] == 0x02)
                {
                    switch (data[1])
                    {
                        case 0x41: CurPitch = BitConverter.ToSingle(data, 2); ; break;
                        case 0x42: CurRoll = BitConverter.ToSingle(data, 2); break;
                        case 0x43: CurYaw = BitConverter.ToSingle(data, 2); break;
                        case 0x44: CurData4 = BitConverter.ToSingle(data, 2); break;
                        case 0x45: CurData5 = BitConverter.ToSingle(data, 2); break;
                        case 0x46: CurData6 = BitConverter.ToSingle(data, 2); break;
                        case 0x47: CurData7 = BitConverter.ToSingle(data, 2); break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < data.Length; i += 8)
                {
                    if (data[i] == 0x02)
                    {
                        switch (data[i + 1])
                        {
                            case 0x41: CurPitch = BitConverter.ToSingle(data, 2); break;
                            case 0x42: CurRoll = BitConverter.ToSingle(data, 2); break;
                            case 0x43: CurYaw = BitConverter.ToSingle(data, 2); break;
                            case 0x44: CurData4 = BitConverter.ToSingle(data, 2); break;
                            case 0x45: CurData5 = BitConverter.ToSingle(data, 2); break;
                            case 0x46: CurData6 = BitConverter.ToSingle(data, 2); break;
                            case 0x47: CurData7 = BitConverter.ToSingle(data, 2); break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
        private void InitChart()
        {
            this.chartShow.ChartAreas.Clear();
            this.chartShow.ChartAreas.Add(chartArea1);
            //定义存储和显示点的容器
            this.chartShow.Series.Clear();

            //Pitch.ChartArea = "C1";
            this.chartShow.Series.Add(Pitch);
            this.chartShow.Series.Add(Roll);
            this.chartShow.Series.Add(Yaw);
            this.chartShow.Series.Add(Data4);
            this.chartShow.Series.Add(Data5);
            this.chartShow.Series.Add(Data6);
            this.chartShow.Series.Add(Data7);
            this.chartShow.Series.Add(Data8);
            //设置图表显示样式
            //this.chartShow.ChartAreas[0].AxisY.Minimum = -180;
            //this.chartShow.ChartAreas[0].AxisY.Maximum = 180;
            this.chartShow.ChartAreas[0].AxisX.Maximum = 280;
            this.chartShow.ChartAreas[0].AxisX.Minimum = 0;
            this.chartShow.ChartAreas[0].AxisX.Interval = 50;
            this.chartShow.ChartAreas[0].AxisY.Interval = 30;
            this.chartShow.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver;
            this.chartShow.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver;
            ////设置标题
            this.chartShow.Titles.Clear();
            this.chartShow.Titles.Add("波形显示");
            this.chartShow.Titles[0].ForeColor = Color.RoyalBlue;
            this.chartShow.Titles[0].Font = new Font("Microsoft Sans Serif", 12F);
            //设置图表显示样式
            this.chartShow.Series[0].Color = Color.Red;
            this.chartShow.Series[1].Color = Color.Aquamarine;
            this.chartShow.Series[2].Color = Color.Black;
            this.chartShow.Series[3].Color = Color.Green;
            this.chartShow.Series[4].Color = Color.Blue;
            this.chartShow.Series[5].Color = Color.Purple;
            this.chartShow.Series[6].Color = Color.Pink;
            this.chartShow.Series[7].Color = Color.Orange;

            if (radioWave.Checked)
            {
                this.chartShow.Titles[0].Text = string.Format("数据 {0}显示", radioPolygonal.Text);
                chartShow.Series[0].ChartType = SeriesChartType.FastLine ;
                chartShow.Series[1].ChartType = SeriesChartType.FastLine;
                chartShow.Series[2].ChartType = SeriesChartType.FastLine;
                chartShow.Series[3].ChartType = SeriesChartType.FastLine;
                chartShow.Series[4].ChartType = SeriesChartType.FastLine;
                chartShow.Series[5].ChartType = SeriesChartType.FastLine;
                chartShow.Series[6].ChartType = SeriesChartType.FastLine;
                chartShow.Series[7].ChartType = SeriesChartType.FastLine;
            }
            this.chartShow.Series[0].Points.Clear();
            this.chartShow.Series[1].Points.Clear();
            this.chartShow.Series[2].Points.Clear();
            this.chartShow.Series[3].Points.Clear();
            this.chartShow.Series[4].Points.Clear();
            this.chartShow.Series[5].Points.Clear();
            this.chartShow.Series[6].Points.Clear();
            this.chartShow.Series[7].Points.Clear();
        }
        private void BtnInit_Click(object sender, EventArgs e)
        {
            InitChart();
        }

        private void BtnStart_Click(object sender, EventArgs e)
        {
            timerPitch.Start();
            timerRoll.Start();
            timerYaw.Start();
            cbPitch.Checked = true;
            cbRoll.Checked = true;
            cbYaw.Checked = true;
            //timerData4.Start();
            //timerData5.Start();
            //timerData6.Start();
            //timerData7.Start();
            //timerData8.Start();
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            timerPitch.Stop();
            timerRoll.Stop();
            timerYaw.Stop();
            timerData4.Stop();
            timerData5.Stop();
            timerData6.Stop();
            timerData7.Stop();
            timerData8.Stop();
        }


        private void UpdatePitchValue()
        {
            if (dataPitch.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    dataPitch.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                dataPitch.Enqueue(getpitch());
            }

        }

        private void UpdateRollValue()
        {
            if (dataRoll.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    dataRoll.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                dataRoll.Enqueue(getroll());
            }
        }

        private void UpdateYawValue()
        {
            if (dataYaw.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    dataYaw.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                dataYaw.Enqueue(getyaw());
            }
        }

        private void UpdateData4Value()
        {
            if (data4.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    data4.Dequeue();
                }
            }

            for (int i = 0; i < num; i++)
            {
                data4.Enqueue(getcompass());
            }
            
        }

        private void UpdateData5Value()
        {
            if (data5.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    data5.Dequeue();
                }
            }

            for (int i = 0; i < num; i++)
            {
                data5.Enqueue(gettemp());
            }
        
        }

        private void UpdateData6Value()
        {

            if (data6.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    data6.Dequeue();
                }
            }

            for (int i = 0; i < num; i++)
            {
                data6.Enqueue(getdepth());
            }
        }

        private void UpdateData7Value()
        {
            if (data7.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    data7.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                data7.Enqueue(getelec());
            }
        }

        private void UpdateData8Value()
        {
            if (data8.Count > 250)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    data8.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                data8.Enqueue(CurData8);
            }
        }

        int i = 0;


        private void Timer1_Tick(object sender, EventArgs e)
        {
            this.chartShow.Series[0].Points.Clear();
            UpdatePitchValue();

            for (int i = 0; i < dataPitch.Count; i++)
            {
                this.chartShow.Series[0].Points.AddXY((i + 1), dataPitch.ElementAt(i));
            }

        }

        private void TimerRoll_Tick(object sender, EventArgs e)
        {
            UpdateRollValue();

            this.chartShow.Series[1].Points.Clear();

            for (int i = 0; i < dataRoll.Count; i++)
            {
                this.chartShow.Series[1].Points.AddXY((i + 1), dataRoll.ElementAt(i));
            }

        }

        private void TimerYaw_Tick(object sender, EventArgs e)
        {
            UpdateYawValue();

            this.chartShow.Series["Yaw"].Points.Clear();

            for (int i = 0; i < dataYaw.Count; i++)
            {
                this.chartShow.Series["Yaw"].Points.AddXY((i + 1), dataYaw.ElementAt(i));
            }
        }

        private void TimerData4_Tick(object sender, EventArgs e)
        {
            UpdateData4Value();

            this.chartShow.Series["Data4"].Points.Clear();

            for (int i = 0; i < data4.Count; i++)
            {
                this.chartShow.Series["Data4"].Points.AddXY((i + 1), data4.ElementAt(i));
            }
        }

        private void TimerData5_Tick(object sender, EventArgs e)
        {
            UpdateData5Value();

            this.chartShow.Series["Data5"].Points.Clear();

            for (int i = 0; i < data5.Count; i++)
            {
                this.chartShow.Series["Data5"].Points.AddXY((i + 1), data5.ElementAt(i));
            }
        }

        private void TimerData6_Tick(object sender, EventArgs e)
        {
            UpdateData6Value();

            this.chartShow.Series["Data6"].Points.Clear();

            for (int i = 0; i < data6.Count; i++)
            {
                this.chartShow.Series["Data6"].Points.AddXY((i + 1), data6.ElementAt(i));
            }
        }

        private void TimerData7_Tick(object sender, EventArgs e)
        {
            UpdateData7Value();

            this.chartShow.Series["Data7"].Points.Clear();

            for (int i = 0; i < data7.Count; i++)
            {
                this.chartShow.Series["Data7"].Points.AddXY((i + 1), data7.ElementAt(i));
            }
        }

        private void TimerData8_Tick(object sender, EventArgs e)
        {
            UpdateData8Value();

            this.chartShow.Series["Data8"].Points.Clear();

            for (int i = 0; i < data8.Count; i++)
            {
                this.chartShow.Series["Data8"].Points.AddXY((i + 1), data8.ElementAt(i));
            }
        }

        private void RadioPolygonal_CheckedChanged(object sender, EventArgs e)
        {
            this.chartShow.Titles[0].Text = string.Format("{0} 显示", radioPolygonal.Text);
            if (radioPolygonal.Checked)
            {
                chartShow.Series[0].ChartType = SeriesChartType.Spline;
                chartShow.Series[1].ChartType = SeriesChartType.Spline;
                chartShow.Series[2].ChartType = SeriesChartType.Spline;
                chartShow.Series[3].ChartType = SeriesChartType.Spline;
                chartShow.Series[4].ChartType = SeriesChartType.Spline;
                chartShow.Series[5].ChartType = SeriesChartType.Spline;
                chartShow.Series[6].ChartType = SeriesChartType.Spline;
                chartShow.Series[7].ChartType = SeriesChartType.Spline;
            }
        }

        private void RadioWave_CheckedChanged(object sender, EventArgs e)
        {
            this.chartShow.Titles[0].Text = string.Format("{0}显示", radioWave.Text);
            if(radioWave.Checked)
            {
                chartShow.Series[0].ChartType = SeriesChartType.FastLine;
                chartShow.Series[1].ChartType = SeriesChartType.FastLine;
                chartShow.Series[2].ChartType = SeriesChartType.FastLine;
                chartShow.Series[3].ChartType = SeriesChartType.FastLine;
                chartShow.Series[4].ChartType = SeriesChartType.FastLine;
                chartShow.Series[5].ChartType = SeriesChartType.FastLine;
                chartShow.Series[6].ChartType = SeriesChartType.FastLine;
                chartShow.Series[7].ChartType = SeriesChartType.FastLine;
            }
        }

        private void CbPitch_CheckedChanged(object sender, EventArgs e)
        {
            if (cbPitch.Checked)
            {
                timerPitch.Start();
            }
            else
            {
                timerPitch.Stop();
                this.chartShow.Series[0].Points.Clear();
            }
        }

        private void WaveForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void CbRoll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbRoll.Checked)
            {
                timerRoll.Start();
            }
            else
            {
                timerRoll.Stop();
                this.chartShow.Series[1].Points.Clear();
            }
        }

        private void CbYaw_CheckedChanged(object sender, EventArgs e)
        {
            if (cbYaw.Checked)
            {
                timerYaw.Start();
            }
            else
            {
                timerYaw.Stop();
                this.chartShow.Series[2].Points.Clear();
            }
        }

        private void CbData4_CheckedChanged(object sender, EventArgs e)
        {
            if (cbData4.Checked)
            {
                timerData4.Start();
            }
            else
            {
                timerData4.Stop();
                this.chartShow.Series[3].Points.Clear();
            }
        }

        private void CbData5_CheckedChanged(object sender, EventArgs e)
        {
            if (cbData5.Checked)
            {
                timerData5.Start();
            }
            else
            {
                timerData5.Stop();
                this.chartShow.Series[4].Points.Clear();
            }
        }

        private void CbData6_CheckedChanged(object sender, EventArgs e)
        {
            if (cbData6.Checked)
            {
                timerData6.Start();
            }
            else
            {
                timerData6.Stop();
                this.chartShow.Series[5].Points.Clear();
            }
        }

        private void CbData7_CheckedChanged(object sender, EventArgs e)
        {
            if (cbData7.Checked)
            {
                timerData7.Start();
            }
            else
            {
                timerData7.Stop();
                this.chartShow.Series[6].Points.Clear();
            }
        }

        private void CbData8_CheckedChanged(object sender, EventArgs e)
        {
            if (cbData8.Checked)
            {
                timerData8.Start();
            }
            else
            {
                timerData8.Stop();
                this.chartShow.Series[7].Points.Clear();
            }
        }

        private void BtnMyline_Click(object sender, EventArgs e)
        {
            CurData8 = float.Parse(tbSetLine.Text);
        }
    }
}
